<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>商品信息管理</title>
		<script type="text/javascript">
			function doDel(id){
				if(confirm("确定删除?")){
					//跳转
					window.location.href="action.php?a=del&id="+id;
				}
			}
		</script>
	</head>
	<body>
		<center>
			<h3>浏览商品信息</h3>
			<!--设置搜索框-->
			<form action="index.php" method="get">
				商品名称：<input type="text" name="comname" size="12">
				商品系列：<select name="state">
							<option value="">-全部-</option>
							<option value="1">新商品</option>
							<option value="2">在售</option>
							<option value="3">已下架</option>
					  </select>
					  <input type="submit" value="搜索">
			</form><br>
			<table width="95%" border="1">
				<tr>
					<th>ID</th>
					<th>商品名称</th>
					<th>商品系列</th>
					<th>商品图片</th>
					<th>单价</th>
					<th>生产厂家</th>
					<th>简介</th>
					<th>库存量</th>
					<th>销售量</th>
					<th>点击量</th>
					<th>状态</th>
					<th>添加时间</th>
					<th>操作</th>
				</tr>
				<?php
					date_default_timezone_set("PRC");//设置时间
					$state = [1=>"新商品",2=>"在售",3=>"已下架"];	
					require("../../public/config.php");		//导入配置文件
					
					$link = mysqli_connect(HOST,USER,PASS,DBNAME) or die("连接数据库失败");
					
					mysqli_set_charset($link,"utf8");	//设置数据库编码
					
					
					//========搜索============================================
					$wherelist = array();//定义一个搜索条件的空数组
					$urllist = array();//定义一个搜索条件的url数组 用于做url参数
					
					//判断获取有没有填搜索条件
					if(!empty($_GET['comname'])){
						$wherelist[] = "goods like '%{$_GET['comname']}%'";
						$urllist[] = "goods={$_GET['comname']}";
					}
					
					if(!empty($_GET['state'])){
						$wherelist[] = "state='{$_GET['state']}'";
						$urllist[] = "state={$_GET['state']}";
					}
					//判断数组是否为空,不为空则拼接sql语句
					if(count($wherelist)>0){
						$where = " where ".implode(" and ",$wherelist);
						$whereli = " and ".implode(" and ",$wherelist);
						$url = "&".implode("&",$urllist);
						
					}
					//========分页======================================
					
					// 分页公式 (当前页-1)*页大小,页大小
					//定义变量
					$page = isset($_GET['p'])?$_GET['p']:1;//当前页
					$pageSize = 4;//页大小
					$maxRows = 0;//总条数
					$maxPage = 0;//总页数
					//获取总条数
					$sql = "select count(*) as num from goods".$where;
					$result = mysqli_query($link,$sql);
					$row = mysqli_fetch_assoc($result);
					$maxRows = $row['num'];		
					//计算总页数
					$maxPage = ceil($maxRows/$pageSize);
					// echo $maxPage;
					//判断页数是否越界
					if($page>$maxPage){
						$page = $maxPage;
					}

					if($page<1){
						$page = 1;
					}
					//拼装limit分页语句
					$limit = " limit ".(($page-1)*$pageSize).",".$pageSize;
					
					
					//4.定义sql语句 并发送
					$sql = "select g.*,t.name from goods g,type t where g.typeid=t.id ".$whereli.$limit;
					$result =  mysqli_query($link,$sql);	
					while($row = mysqli_fetch_assoc($result)){	//遍历并输出
						echo "<tr>";
						echo "<td>{$row['id']}</td>";
						echo "<td>{$row['goods']}</td>";
						echo "<td>{$row['name']}</td>";
						echo "<td><img src='../../public/uploads/s_{$row['picname']}'></td>";
						echo "<td>{$row['price']}</td>";
						echo "<td>{$row['company']}</td>";
						echo "<td>{$row['descr']}</td>";
						echo "<td>{$row['store']}</td>";
						echo "<td>{$row['num']}</td>";
						echo "<td>{$row['clicknum']}</td>";
						echo "<td>{$state[$row['state']]}</td>";
						echo "<td>".date("Y-m-d H:i:s",$row['addtime'])."</td>";
						echo "<td><a href='edit.php?id={$row['id']}'>修改</a>
								<a href='javascript:doDel({$row['id']})'>删除</a></td>";
						echo "</tr>";
					}
					
				?>
			</table>
			<br></br>
			<?php 
				//输出页码信息
				echo "当前第{$page}/{$maxPage}页 共计{$maxRows}条"; 
				echo "<a href='index.php?p=1{$url}'>首页</a>";
				echo "<a href='index.php?p=".($page-1)."{$url}'>上一页</a>";
				echo "<a href='index.php?p=".($page+1)."{$url}'>下一页</a>";
				echo "<a href='index.php?p={$maxPage}{$url}'>末页</a>";
				echo "<br><br>";
				echo "第";
				for($i=1;$i<=$maxPage;$i++){
					echo " <a href='index.php?p={$i}{$url}'>{$i}</a> ";
				}
				echo "页";
				?>
		</center>
	</body>
</html>